package service;

import bean.AddCenter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dao.AdminDao;
import domain.Admin;
import domain.Permission;
import domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class AdminService implements UserDetailsService {
    @Autowired
    private AdminDao adminDao;

    @Autowired
    private BCryptPasswordEncoder encoder;

    //对用户进行认证授权
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<SimpleGrantedAuthority> list = new ArrayList<>();
        Admin admin = adminDao.findByUsername(username);
        List<Role> roles = admin.getRoles();
        for (Role role:roles){
            List<Permission> permissions = role.getPermissions();
            for (Permission permission:permissions){
                SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(permission.getPermissionName());
                list.add(simpleGrantedAuthority);
            }
        }
        User user = new User(admin.getUsername(),admin.getPassword(),admin.isStatus(),true,true,true,list);
        return user;
    }

    //分页查询所有用户
    public PageInfo<Admin> findAllAdmin(int pageNum,int pageSize,Admin admin){
        PageHelper.startPage(pageNum, pageSize);
        admin.setUsername("%"+admin.getUsername()+"%");
        List<Admin> all = adminDao.findAll(admin);
        PageInfo pageInfo = new PageInfo(all);
       return pageInfo;
    }
    //新增用户
    public void addAdmin(Admin admin,int[] role){
        admin.setPassword(encoder.encode(admin.getPassword()));
        adminDao.add(admin);
        int id = admin.getId();
        for (int role1:role){
            AddCenter addCenter = new AddCenter();
            addCenter.setNum1(id);
            addCenter.setNum2(role1);
            adminDao.addAdim_Role(addCenter);
        }
    }
    //根据id查询
    public Admin findById(int id){
        return adminDao.findById(id);
    }
    //改变状态
    public void updataAdminStatus(boolean status,int[] id ){
        Admin admin = new Admin();
        for (int i:id){
           admin.setId(i);
           admin.setStatus(status);
           adminDao.updateStatus(admin);
        }
    }

    //修改信息
    public void updateAdmin(Admin admin,int[] role){
        adminDao.deleteAdminRole(admin.getId());
        adminDao.update(admin);
        AddCenter addCenter = new AddCenter();
        for (int i:role){
           addCenter.setNum1(admin.getId());
           addCenter.setNum2(i);
           adminDao.addAdim_Role(addCenter);
        }
    }

    //修改密码
    public void updateAdminPassword(Admin admin){
        admin.setPassword(encoder.encode(admin.getPassword()));
        adminDao.updatePassword(admin);
    }
}
